#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/7/29 14:59
# @Author  : 王凯
# @File    : pdd_app_fav.py
# @Project : scrapy_spider
import concurrent.futures
import datetime
import random
import time

import requests
from loguru import logger

from components.config import WFQ_SOURCE_MYSQL_CONFIG
from utils.db.mysqldb import MysqlDB


class PddAppFav(object):

    def __init__(self, device_id="EQLNG0vV"):
        self.session = requests.Session()
        self._to_db = None
        self.device_id = device_id

    @property
    def to_db(self):
        if self._to_db is None:
            self._to_db = MysqlDB(
                ip=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_IP"],
                port=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_PORT"],
                db=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_DB"],
                user_name=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_USER_NAME"],
                user_pass=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_USER_PASS"],
            )
        return self._to_db

    def like_mall(self, mall_id):
        res = self.get_like_mall_response(mall_id)
        if not res:
            url = f"http://159.75.108.223:5612/business/invoke?group={self.device_id}&action=PddMallHandler&like_mall_id={mall_id}"
            self.session.get(url, verify=False)
            time.sleep(random.randint(5, 10))
            res = self.get_like_mall_response(mall_id)
        logger.info("like mall: {},  res: {}".format(mall_id, res))
        if res.get("error_code"):
            raise Exception(res)
        if res.get("server_time") == 0:
            raise Exception(res)

    def get_like_mall_response(self, mall_id):
        data_create_time = (datetime.datetime.now() - datetime.timedelta(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")
        sql = f"""select response from net_pdd_proxy_log where mall_id = '{mall_id}' and source = 'favorite' and create_time > '{data_create_time}' order by id desc limit 1"""
        response = self.to_db.find(sql, to_json=True)
        if response:
            response = response[0]["response"]
        return response

    def get_fav_list(self, page=1):
        res = self.get_response_from_database(page)
        if not res:
            time.sleep(random.randint(5, 20))
            url = f"http://159.75.108.223:5612/business/invoke?group={self.device_id}&action=PddMallHandler&like_page_num={page}"
            self.session.get(url, verify=False)
            time.sleep(random.randint(5, 20))
            res = self.get_response_from_database(page)
        logger.info("page: {}, res: {}".format(page, res))
        return res

    def get_response_from_database(self, page_no):
        data_create_time = (datetime.datetime.now() - datetime.timedelta(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")
        sql = f"""select response from net_pdd_proxy_log where mall_id = '00000000' and source = 'favorite' and page_no='{page_no}' and create_time > '{data_create_time}' order by id desc limit 1"""
        response = self.to_db.find(sql, to_json=True)
        if response:
            response = response[0]["response"]
        return response

    def parse_data(self, response):
        """
        {"mall_list": [{"logo": "http://t16img.yangkeduo.com/pdd_ims/img_check/v2/FFF0E6E6882C0120200112155259495/0a256d43a6b34aa8bf03aa093ecc96a7.png", "is_open": 1, "mall_id": 136890254, "mall_name": "三泰百货商行", "brand_name": "三泰百货商行", "mall_sales": 0, "router_info": {"pdd_router": "mall_page.html?mall_id=136890254&msn=zsttqscsn6zarq3mbuv6fxture_axbuy&_sop_rcto=YWJWUnSPfxhEM-5eQAIb4Ufx", "pdd_router_name": "进店逛逛"}, "mall_icon_bos": [], "mall_goods_num": 12, "mall_coupon_available": 0}, {"logo": "http://t16img.yangkeduo.com/pdd_ims/15fc77c3608d291f5e4325f98fe39d7e.jpg", "is_open": 1, "mall_id": 1283012, "mall_name": "保护伞酒品批发", "brand_name": "保护伞酒品批发", "mall_sales": 0, "router_info": {"pdd_router": "mall_page.html?mall_id=1283012&msn=hfeslyrtiq5cpdypqfwaknuvda_axbuy&_sop_rcto=YWKSQ2JBItDfZkZcl978JPak", "pdd_router_name": "进店逛逛"}, "mall_icon_bos": [{"url": "https://promotion.pddpic.com/oms-img-promotion/2024-12-15/851c675c-d0bc-4c55-b799-77172b59d9de.png", "width": 168, "height": 48, "icon_type": 4}], "mall_goods_num": 49, "mall_coupon_available": 0}, {"logo": "https://img.pddpic.com/goods_mms/2024-04-25/b1fe789f-6f6d-48df-b671-d68a5fcbe50f_suffix.jpeg", "is_open": 1, "mall_id": 127691522, "mall_name": "精美礼品工厂", "brand_name": "精美礼品工厂", "mall_sales": 0, "router_info": {"pdd_router": "mall_page.html?mall_id=127691522&msn=meqctnuwevgpgbedigmqr7l4e4_axbuy&_sop_rcto=YWI-c61zSwUbmSg58cL1D6J1", "pdd_router_name": "进店逛逛"}, "mall_icon_bos": [], "mall_goods_num": 9, "mall_coupon_available": 0}, {"logo": "http://t16img.yangkeduo.com/pdd_ims/img_check/v2/E62F2CFFFFFF0720200112154059921/64f05cb4f53945b7b23468d0a222f6ba.png", "is_open": 1, "mall_id": 126023667, "mall_name": "芒果小猪", "brand_name": "芒果小猪", "mall_sales": 0, "router_info": {"pdd_router": "mall_page.html?mall_id=126023667&msn=vvzjdgocospa7tpuicmwgn3rfi_axbuy&_sop_rcto=YWKn8Ad_SU0kU_a1G77Cwzpz&has_decoration=1", "pdd_router_name": "进店逛逛"}, "mall_icon_bos": [], "mall_goods_num": 121, "mall_coupon_available": 0}, {"logo": "https://img.pddpic.com/goods_mms/2024-12-26/f2edee14-10c8-4727-9258-b31132071097.jpeg.a.jpeg", "is_open": 1, "mall_id": 125966482, "mall_name": "花喜子", "brand_name": "花喜子", "mall_sales": 0, "router_info": {"pdd_router": "mall_page.html?mall_id=125966482&msn=ej2apqv75bpf7zcdj47xlnjycm_axbuy&_sop_rcto=YWI0Pt1GQEU2FMqbOffDQVfc", "pdd_router_name": "进店逛逛"}, "mall_icon_bos": [], "mall_goods_num": 5, "mall_coupon_available": 0}, {"logo": "http://t00img.yangkeduo.com/goods/images/2019-02-13/8beb68a6-63b5-4ced-b6b8-fe4368f0bc6a.jpg", "is_open": 1, "mall_id": 1198045, "mall_name": "河北奥诺体育", "brand_name": "河北奥诺体育", "mall_sales": 0, "router_info": {"pdd_router": "mall_page.html?mall_id=1198045&msn=zlnblnnwi4v7mruzsk2n5gyjpm_axbuy&_sop_rcto=YWIbFSURXi3njvCrt6Ptv0um&has_decoration=1", "pdd_router_name": "进店逛逛"}, "mall_icon_bos": [], "mall_goods_num": 15, "mall_coupon_available": 0}, {"logo": "http://t16img.yangkeduo.com/pdd_ims/mainObject/v1/pub_202206178fc2e099193647369528ccffca652055.jpg", "is_open": 1, "mall_id": 1178672, "mall_name": "久久游", "brand_name": "久久游", "mall_sales": 0, "router_info": {"pdd_router": "mall_page.html?mall_id=1178672&msn=ahqzxhj4b44nyy4aynnlkqkiey_axbuy&_sop_rcto=YWL_A8R1HTwgZPt-6Tpry2CP", "pdd_router_name": "进店逛逛"}, "mall_icon_bos": [], "mall_goods_num": 5, "mall_coupon_available": 0}, {"logo": "http://t16img.yangkeduo.com/pdd_ims/2020-12-05/63035631-a981-4979-969f-4ae86a178899.jpeg", "is_open": 1, "mall_id": 112421796, "mall_name": "晨洁家居生活馆", "brand_name": "晨洁家居生活馆", "mall_sales": 0, "router_info": {"pdd_router": "mall_page.html?mall_id=112421796&msn=grsxzluii5em4derz2per2ogzu_axbuy&_sop_rcto=YWLgGz05okmKrNSMv3sIchkH&has_decoration=1", "pdd_router_name": "进店逛逛"}, "mall_icon_bos": [{"url": "https://promotion.pddpic.com/oms-img-promotion/2024-12-15/851c675c-d0bc-4c55-b799-77172b59d9de.png", "width": 168, "height": 48, "icon_type": 4}], "mall_goods_num": 226, "mall_coupon_available": 0}, {"logo": "https://img.pddpic.com/goods_mms/2024-07-17/ce11e90b-cf26-4cde-84f5-405aaf838c14_suffix.jpeg", "is_open": 1, "mall_id": 101457548, "mall_name": "坦博尔童装官方旗舰店", "brand_name": "坦博尔童装官方旗舰店", "mall_sales": 0, "router_info": {"pdd_router": "mall_page.html?mall_id=101457548&msn=mdptuew5rdzom5xr3blxbe7zrq_axbuy&_sop_rcto=YWKyp0act8s8SRn5Lvf0Uohk&has_decoration=1", "pdd_router_name": "进店逛逛"}, "mall_icon_bos": [{"url": "https://funimg.pddpic.com/d66b3a03-0041-482b-82c7-ab9d5f9f1603.png", "width": 90, "height": 48, "icon_type": 1}, {"url": "https://promotion.pddpic.com/oms-img-promotion/2024-12-15/851c675c-d0bc-4c55-b799-77172b59d9de.png", "width": 168, "height": 48, "icon_type": 4}], "mall_goods_num": 51, "mall_coupon_available": 0}, {"logo": "https://t00img.yangkeduo.com/goods/images/2020-10-26/3af6045b-03f8-49f6-8084-5ac4204bf625_suffix.jpeg", "is_open": 1, "mall_id": 210584197, "mall_name": "乔乐乐食品店", "brand_name": "乔乐乐食品店", "mall_sales": 0, "router_info": {"pdd_router": "mall_page.html?mall_id=210584197&msn=3fb2al7wzajnn6tjr33ll6uxxi_axbuy&_sop_rcto=YWIzEO-p8YBhJexoXWfyJPQ3&has_decoration=1", "pdd_router_name": "进店逛逛"}, "mall_icon_bos": [], "mall_goods_num": 10, "mall_coupon_available": 0}], "server_time": 1735613254, "favorite_mall_update_time": 1735287499}
        """
        datas = response.get("mall_list", [])
        result = []
        for data in datas:
            mall_id = data["mall_id"]
            shop_name = data["mall_name"]
            is_open = data["is_open"]
            mall_goods_num = data["mall_goods_num"]
            mall_sales = data["mall_sales"]
            pdd_route = data["router_info"]["pdd_router"]
            params = dict([i.split("=") for i in pdd_route.split("?")[1].split("&")])
            item = {
                "mall_id": mall_id,
                "shop_name": shop_name,
                "msn": params.get("msn"),
                "_sop_rcto": params.get("_sop_rcto"),
            }
            # if is_open == 0 and mall_goods_num == 0 and mall_sales == 0:
            #     item["flag"] = -1
            #     item["shop_status"] = "该店铺已失效"
            result.append(item)
        return result

    def save_datas(self, datas):
        with concurrent.futures.ThreadPoolExecutor(max_workers=10) as e:
            for i in datas:
                e.submit(
                    self.to_db.update_smart,
                    "net_pdd_shop_info",
                    i,
                    condition=f"mall_id='{i['mall_id']}'",
                )

    def run(self):
        page = 1
        response = self.get_fav_list(page=page)
        datas = response.get("mall_list")
        if len(datas) < 20:
            has_more = False
        else:
            has_more = True
        self.save_datas(self.parse_data(response))
        while has_more is True:
            page += 1
            response = self.get_fav_list(page=page)
            datas = response.get("mall_list")
            if len(datas) < 20:
                has_more = False
            self.save_datas(self.parse_data(response))

    def get_mall_ids(self):
        sql = "select mall_id from net_pdd_shop_info where msn is null limit 10"
        result = self.to_db.find(sql, to_json=True)
        if result:
            mall_ids = [i["mall_id"] for i in result]
            return mall_ids
        return []

    def run_forever(self):
        while True:
            sub_mall_ids = self.get_mall_ids()
            for mall_id in sub_mall_ids:
                self.like_mall(mall_id=mall_id)
            self.run()
            time.sleep(60 * 60 + random.randint(5, 20) * 60)


if __name__ == "__main__":
    fav = PddAppFav()
    fav.run_forever()
